package system;

import Main.*;
import java.io.File;
import java.io.FileWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Read message locale file specified in settings.ini
 * @author ZeM
 */
public class Message {

    public String message;

    /**
     * @param mes type and number of message
     * @param message message from locale file
     */
    public Message(String mes) {

        try {
            
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("locale"+File.separatorChar+"locale."+Parameters.Language+".xml"));
            // normalize text representation
            doc.getDocumentElement().normalize();

//reading  parameters

            NodeList listOfTasks = doc.getElementsByTagName("messages");
            int totalTasks = listOfTasks.getLength();
            for (int tc = 0; tc < totalTasks; tc++) {
                Node firstPersonNode = listOfTasks.item(tc);
                if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element firstPersonElement = (Element) firstPersonNode;
                    boolean flag = true; //mean that everithing is OK
                    //----description
                    NodeList nodelist = firstPersonElement.getElementsByTagName(mes);
                    int length = nodelist.getLength();
                    if (length > 0) {
                        Element nelement = (Element) nodelist.item(0);
                        NodeList textList = nelement.getChildNodes();
                        message =((Node) textList.item(0)).getNodeValue().trim();
                        
                       
                        ///////////////////////////////////////////////////////////////////
                        //!!!!!!!!!!!!!!!!! ONLY for WINDOWS !!!!!!!!!!!!!!!!!!!!!!!!!!!!//
                        ///////////////////////////////////////////////////////////////////
                        if(Parameters.LounchedFromWindows){
                        message=StringToCp1251(message);
                        }
                        //////////////////////////////////////////////////////////////////////
                        //                                                                  //
                        //////////////////////////////////////////////////////////////////////
                        char[] tmp = message.toCharArray();
                        if (tmp[0] == '"') {
                            if (tmp[tmp.length - 1] == '"') {
                                message = "";
                                boolean parameterflag = false;
                                for (int i = 1; i < tmp.length - 1; i++) {
                                    if (!parameterflag) {
                                        if (tmp[i] == '$') {
                                            parameterflag = true;
                                        } else {
                                            message += tmp[i];
                                        }
                                    } else {
                                        parameterflag = false;
                                        message += Parameters.message_parameter[Integer.parseInt(""+tmp[i])];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception er) {
        }
    }
    public String StringToCp1251(String string){
        /*Converts string to cp1251 (used only for windows)
         */
        try{
     byte[] tmp1=string.getBytes("Cp866");

     string = new String(tmp1,"Cp1251");
        }catch (Exception er){}
        return string;
    }
}
